package com.dragon.string;

/**
 * @author dragon
 * @since 2019/12/23
 */
public class Test2 {

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2.strStr("", ""));
    }

    public int strStr(String haystack, String needle) {
        if (haystack == null || needle == null || haystack.length() < needle.length()) {
            return -1;
        }
        if ("".equals(needle)) {
            return 0;
        }
        for (int i = 0, j = 0; i < haystack.length() && j < needle.length(); ) {
            char num = haystack.charAt(i);
            char c = needle.charAt(j);
            if (num != c) {
                i++;
            } else {
                boolean flag = true;
                int m = j;
                int n = i;
                while (++m < needle.length() && ++n < haystack.length()) {
                    num = haystack.charAt(n);
                    c = needle.charAt(m);
                    if (num != c) {
                        flag = false;
                        break;
                    }
                }
                if (flag && m == needle.length()) {
                    return i;
                }
            }
        }

        return -1;

    }
}
